package com.ctshk.rpc.workflow.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.workflow.dto.ApprovalDTO;
import com.ctshk.rpc.workflow.dto.ApprovalInfoDTO;
import com.ctshk.rpc.workflow.entity.SysApproval;
import com.ctshk.rpc.workflow.req.ApprovalReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 审批 Mapper 接口
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-25
 */
public interface SysApprovalMapper extends BaseMapper<SysApproval> {

    /**
     * 审批中心-由我发起-列表
     * @param page
     * @param req
     * @return
     */
    @Select("<script>"+
            "SELECT" +
            " tsa.id, tsa.approval_number, tsa.sponsor_name, tsa.gmt_create, tsa.status, tsa.business_id, tsas.approval_type " +
            "FROM " +
            " `t_sys_approval` tsa " +
            " LEFT JOIN t_sys_approval_setting tsas ON tsa.setting_id = tsas.id  " +
            "WHERE " +
            " tsa.sponsor_id in (${req.sponsorId}) " +
            "<if test=\"req.approvalNumber != null and req.approvalNumber != ''\"> AND tsa.approval_number LIKE CONCAT('%',#{req.approvalNumber},'%') </if>" +
            "<if test=\"req.status != null\"> AND tsa.`status` = #{req.status} </if>" +
            "<if test=\"req.startTime != null and req.endTime != ''\"> AND tsa.gmt_create between #{req.startTime} and #{req.endTime} </if>" +
            "<if test=\"req.approvalType != null\">  AND tsas.approval_type = #{req.approvalType} </if>" +
            " AND tsa.is_deleted = 0" +
            " GROUP BY tsa.id" +
            " ORDER BY tsa.gmt_create DESC"
            +"</script>")
    IPage<ApprovalDTO> selectListPage(IPage<ApprovalDTO> page, @Param("req") ApprovalReq req);


    /**
     * 审批中心-由我审批/我已审批-列表
     * @param page
     * @param req
     *          type == 2 由我审批列表
     *          type == 3 我已审批列表
     * @return
     */
    @Select("<script>"+
            "SELECT" +
            " tsa.id, tsa.approval_number, tsa.sponsor_name, tsa.gmt_create, tsa.status, tsa.business_id, tsas.approval_type" +
            " FROM" +
            " `t_sys_approval` tsa" +
            " LEFT JOIN `t_sys_approval_setting` tsas ON tsa.setting_id = tsas.id " +
            " LEFT JOIN `t_sys_approval_step` tsasp ON tsa.current_step_id = tsasp.id" +
            " LEFT JOIN `t_sys_approval_member` tsam ON tsam.step_id = tsasp.id " +
            "WHERE" +
            " tsam.approver_id in (${req.sponsorId}) " +
            "<if test=\"req.type == 2\">  AND tsasp.is_approval = 0 </if>" +
            "<if test=\"req.type == 3\">  AND tsasp.is_approval = 1 </if>" +
            "<if test=\"req.approvalNumber != null and req.approvalNumber != ''\"> AND tsa.approval_number LIKE CONCAT('%',#{req.approvalNumber},'%') </if>" +
            "<if test=\"req.status != null\"> AND tsa.`status` = #{req.status} </if>" +
            "<if test=\"req.startTime != null and req.endTime != ''\"> AND tsa.gmt_create between #{req.startTime} and #{req.endTime} </if>" +
            "<if test=\"req.approvalType != null\">  AND tsas.approval_type = #{req.approvalType} </if>"+
            " AND tsa.status &lt;&gt; 0 AND tsa.is_deleted = 0" +
            " GROUP BY tsa.id" +
            " ORDER BY tsa.gmt_create DESC"
            +"</script>")
    IPage<ApprovalDTO> selectListPageMyApproval(IPage<ApprovalDTO> page, @Param("req") ApprovalReq req);

    /**
     * 审批中心-审批详情
     * @param id
     * @param approverId
     * @return
     */
    @Select("SELECT" +
            " tsa.*," +
            " tsas.approval_type," +
            " tsas.approval_type_name," +
            " tsasp.is_approval," +
            " IF(tsa.sponsor_id = #{userId},TRUE,FALSE) isMe " +
            "FROM `t_sys_approval` tsa" +
            " LEFT JOIN `t_sys_approval_setting` tsas ON tsa.setting_id = tsas.id" +
            " LEFT JOIN `t_sys_approval_step` tsasp ON tsa.current_step_id = tsasp.id" +
            " LEFT JOIN `t_sys_approval_member` tsam ON tsam.step_id = tsasp.id " +
            "WHERE" +
            " tsa.id = #{id}" +
            " AND tsa.is_deleted = 0" +
            " GROUP BY tsa.id")
    ApprovalInfoDTO selectById( @Param("id")long id, @Param("userId") long userId);
}
